package com.e2oto.seller.modules.launch.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.R;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseActivity;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.http.ProgressSubscriber;
import com.e2oto.seller.common.http.ProgressSubscriberOnNextListener;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.launch.bean.LoginBean;
import com.e2oto.seller.widget.LoginEditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ZhangLiQiang on 2017/11/4.
 */

public class SetPasswordActivity extends BaseActivity {
    @BindView(R.id.reset_password_first_et)
    LoginEditText firstEt;
    @BindView(R.id.reset_password_second_et)
    LoginEditText secondEt;

    private String code;
    private String mobile;

    @Override
    protected void afterView(Bundle savedInstanceState) {
        code = getIntent().getStringExtra("code");
        mobile=getIntent().getStringExtra("mobile");
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_set_password;
    }

    @OnClick({R.id.toolbar_left_btn, R.id.set_password_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.set_password_commit:
                String firstPwd = firstEt.getEditText().getText().toString().trim();
                String secondPwd = secondEt.getEditText().getText().toString().trim();
                if (!TextUtils.equals(firstPwd, secondPwd)) {
                    showMessage("您输入的密码不一致");
                    return;
                }
                resetPassword(code,mobile,firstPwd);
                break;
        }
    }

    private void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    private void resetPassword(String code, String mobile, String password) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("mobile", mobile);
        jsonObject.put("pwd", password);
        String param = EasyAES.encrypt(jsonObject);

        RetrofitUtils.getApiService().resetPasswdForMsg(param, SellerApp.versionName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(new ProgressSubscriberOnNextListener<String>() {
                    @Override
                    public void onNext(String o) {
                        showMessage("修改密码成功");
                        finish();
                    }
                }, this));
    }

}
